package com.example.order.activity;

import android.content.Intent;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.example.order.R;
import com.example.order.adapter.FoodAdapter;
import com.example.order.pojo.Customer;
import com.example.order.pojo.Food;
import com.example.order.utils.Utils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class Main3Activity extends AppCompatActivity {
    //orderid和customer_id
    String order_id;
    String customer_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        final ListView list_food=findViewById(R.id.list_food);
        final ListView list_order=findViewById(R.id.list_order);
        final List<String> lists=new ArrayList<>();
        //记录菜品id和价格
        final Map<Integer,Integer> map=new LinkedHashMap<Integer, Integer>();
        Button button_pay=findViewById(R.id.button_pay);
        Intent intent = getIntent();
        order_id=intent.getStringExtra("order_id");
        Customer customer=(Customer)intent.getSerializableExtra("customer");
        customer_id=String.valueOf(customer.getId());
        Log.i("Main3Activity",customer_id+"  "+order_id);






        /*for (int i = 0; i < 20; i++) {
            list.add("待开发");
        }*/
        final ArrayAdapter<String> adapter2=new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,lists);
        list_order.setAdapter(adapter2);
        //菜品信息,从服务器获取 food/findFoods
        Utils.sendHttpRequest("food/findFoods", new Callback() {
            @Override
            public void onFailure(@NonNull Call call,@NonNull IOException e) {
                Log.e("Main3Activity", Arrays.toString(e.getStackTrace()));
            }

            @Override
            public void onResponse(@NonNull Call call,@NonNull Response response) throws IOException {
                assert response.body() != null;
                String strings=response.body().string();
                Log.i("Main3Activity", strings);
                Gson gson=new Gson();
                final List<Food> list=gson.fromJson(strings, new TypeToken<List<Food>>(){}.getType());
                Log.i("Main3Activity", "list->"+list.toString());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        final FoodAdapter adapter=new FoodAdapter(list, Main3Activity.this, new FoodAdapter.Callback() {

                            @Override
                            public void click(View v) {
                                switch(v.getId())
                                {
                                    case R.id.food_btn1:
                                        //加1
                                        Integer id1=(Integer) v.getTag();
                                        if(!map.containsKey(id1))
                                            map.put(id1,1);
                                        else
                                        {
                                            int temp=map.get(id1);
                                            map.put(id1,temp+1);
                                        }

                                        //更新order list
                                        lists.clear();
                                        Set<Integer> sets = map.keySet();
                                        for (Integer set:sets) {
                                           lists.add("菜品编号"+set+"数量"+map.get(set));
                                        }


                                        adapter2.notifyDataSetChanged();

                                        //Utils.showToast(Main3Activity.this,list.get((int)v.getTag()).toString()+"+1",false);
                                        //Utils.showToast(Main3Activity.this,v.getId()+"+1",false);
                                        break;
                                    case R.id.food_btn2:
                                        //减1
                                        Integer id2=(Integer) v.getTag();
                                        if(!map.containsKey(id2))
                                        {
                                           // map.put(id2,0);
                                            Utils.showToast(Main3Activity.this,"数量不能少于1",false);
                                        }
                                        else
                                        {
                                            int temp=map.get(id2);
                                            if(temp==1)
                                            {
                                                //删除
                                                map.remove(id2);

                                            }
                                            else
                                            map.put(id2,temp-1);
                                        }
                                        lists.clear();
                                        Set<Integer> sets2 = map.keySet();
                                        for (Integer set:sets2) {
                                            //lists.add("id"+set+"count"+map.get(set));
                                            lists.add("菜品编号"+set+"数量"+map.get(set));
                                        }
                                        adapter2.notifyDataSetChanged();
                                        //Utils.showToast(Main3Activity.this,list.get((int)v.getTag()).toString()+"-1",false);
                                        //Utils.showToast(Main3Activity.this,v.getId()+"-1",false);
                                        break;
                                    default:
                                        break;
                                }
                            }

                        });
                        list_food.setAdapter(adapter);
                    }
                });
            }
        });

        button_pay.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onClick(View v) {
                /*Utils.showToast(Main3Activity.this,map.toString(),false);
                //给服务器发送数据,获取总金额
                Set<Integer> set = map.keySet();*/
                if(map.size()!=0&&!map.isEmpty())
                {
                    Set<Integer> set=map.keySet();
                    for(Integer sets:set)
                    {
                        HashMap<String,String> temp=new HashMap<>();
                        temp.put("food_id",String.valueOf(sets));
                        temp.put("count",String.valueOf(map.get(sets)));
                        temp.put("customer_id",customer_id);
                        temp.put("order_id",order_id);
                        Log.i("Main3Activity",temp.toString());
                        Utils.sendPostHttpRequest("orderList/order",
                                temp,
                                new Callback() {
                                    @Override
                                    public void onFailure(Call call, IOException e) {

                                    }

                                    @Override
                                    public void onResponse(Call call, Response response) throws IOException {

                                        String string = response.body().string();
                                        Log.i("Main3Activity",string);

                                    }
                                });
                    }
                    Intent intent=new Intent(Main3Activity.this,Main4Activity.class);
                    intent.putExtra("order_id",order_id);
                    startActivity(intent);
                }




            }
        });
    }
}
